package com.example.translateappjava.DataBase;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class CollectDbHelper extends SQLiteOpenHelper {
    private static CollectDbHelper sHelper;
    private static final String DB_NAME = "collect.db";   //数据库名
    private static final int VERSION = 1;    //版本号

    //必须实现其中一个构方法
    public CollectDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    //创建单例，供使用调用该类里面的的增删改查的方法
    public synchronized static CollectDbHelper getInstance(Context context) {
        if (null == sHelper) {
            sHelper = new CollectDbHelper(context, DB_NAME, null, VERSION);
        }
        return sHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建table表
        db.execSQL("create table collect_table(collect_id integer primary key autoincrement, " +
                "uniquekey text," +       //
                "username text," +       //用户名
                "new_json text" +       //
                ")");


    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public int addCollect(String username, String uniquekey, String new_json) {
        //if added
        if(!isCollect(uniquekey)){
            //获取SQLiteDatabase实例
            SQLiteDatabase db = getWritableDatabase();
            ContentValues values = new ContentValues();
            //填充占位符
            values.put("username", "NULL");
            values.put("uniquekey", uniquekey);
            values.put("new_json", new_json);
            String nullColumnHack = "values(null,?,?,?)";
            //执行
            int insert = (int) db.insert("collect_table", nullColumnHack, values);
//        db.close();
            return insert;
        }
        return 0;


    }
    @SuppressLint("Range")
    public boolean isCollect(String uniquekey) {
        //获取SQLiteDatabase实例
        SQLiteDatabase db = getReadableDatabase();
        String sql = "select collect_id,uniquekey,username,new_json from collect_table where uniquekey=?";
        String[] selectionArgs = {uniquekey};
        Cursor cursor = db.rawQuery(sql, selectionArgs);

        return  cursor.moveToNext();
    }

    /**
     * 获取
     */
    @SuppressLint("Range")
    public List<CollectInfo> queryCollectListData(String username) {
        //获取SQLiteDatabase实例
        SQLiteDatabase db = getReadableDatabase();
        List<CollectInfo> list = new ArrayList<>();
        String sql;
        Cursor cursor;
        if(username == null){
            sql = "select collect_id,uniquekey,username,new_json  from collect_table";
            cursor = db.rawQuery(sql, null);
        }else {
            sql = "select collect_id,uniquekey,username,new_json  from collect_table where username = ?";
            cursor = db.rawQuery(sql, new String[]{username});
        }
        cursor = db.rawQuery(sql, new String[]{username});
        while (cursor.moveToNext()) {
            int collect_id = cursor.getInt(cursor.getColumnIndex("collect_id"));
            String uniquekey = cursor.getString(cursor.getColumnIndex("uniquekey"));
            String userName = cursor.getString(cursor.getColumnIndex("username"));
            String new_json = cursor.getString(cursor.getColumnIndex("new_json"));
            list.add(new CollectInfo(collect_id, uniquekey, userName, new_json));
        }
        cursor.close();
//        db.close();
        return list;
    }
}
